package face.pay.web.withdraw.controller;


import face.pay.common.withdraw.IWithdrawManageService;
import face.pay.constant.UserBizType;
import face.pay.entity.CashRecodeInfo;
import face.pay.entity.UserInfo;
import face.pay.redis.RedisUtils;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/withdrawManage")
@Slf4j
public class WithdrawManageController {
    @Autowired
    private IWithdrawManageService iWithdrawManageService;

    @Autowired
    RedisUtils redisUtils;

    @RequestMapping("/list")
    public String list() {
        return "withdraw/list";
    }


    /**
     * 分页查询提现记录
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("/queryData")
    @ResponseBody
    public PageData queryData(@CurrentUser UserInfo loginUser, CashRecodeInfo withdrawOrderInfo, PageParam pageParam) {


        return this.iWithdrawManageService.queryData(withdrawOrderInfo, pageParam, loginUser);

    }


    /**
     * 我的钱包余额
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("/account")
    @ResponseBody
    public Map account(@CurrentUser UserInfo loginUser) {

        return this.iWithdrawManageService.Account(loginUser);

    }


    /**
     * 发起提现
     *
     * @param loginUser
     * @param amount    发起提现的金额单位元
     * @return
     */
    @RequestMapping("/withdraw")
    @ResponseBody
    public ResultMessage withdraw(@CurrentUser UserInfo loginUser, double amount, String validateCode) throws Exception {
        //校验验证码
        String codeKey = UserBizType.USER_INFO_MODIFY + ":" + loginUser.getId();
        String redisCode = (String) redisUtils.get(codeKey);
        if (redisCode == null || !redisCode.equalsIgnoreCase(validateCode)) {
            return ResultMessage.createFail("验证码校验不正确");
        }
        redisUtils.delete(codeKey);
        return this.iWithdrawManageService.withdraw(loginUser, amount);

    }
}
